const Model = require('./model')
const BaseMongo = require('../base/index')
const { stateKeys } = require('../base/model')

/**
 * Banner
 */
class BannerMongo extends BaseMongo{

  constructor() {
    super()
  }

	/**
	 * 获取所有banner
	 * @returns {Promise<void>}
	 */
  static findAll() {
    return Model.find({ $or: [
				{stateKey: stateKeys.NORMAL},
				{stateKey: {$exists: false}}
			]}).sort({'index': 1}).exec()
  }

	/**
	 * 修改banner
	 * @param data banner数据
	 * @returns {Promise<*>}
	 */
	static update(data) {
		return Model.updateOne(
			{_id: this.objId(data.id)},
			this.getJson(data, ['title', 'detail', 'index', 'switch', 'pic']),
			{
				multi: true
			})
	}
}

/**
 * 设置当前模型
 */
BannerMongo.model = Model
/**
 * 添加字段数组
 * @type {Array}
 */
BaseMongo.fieldArr = ['title', 'detail', 'index', 'switch', 'pic']

module.exports = BannerMongo
